import tensorflow as tf

# 加载数据集
(train_image, train_label), (test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()

train_image = train_image / 255
test_image = test_image / 255

ds_train_image = tf.data.Dataset.from_tensor_slices(train_image)
ds_train_label = tf.data.Dataset.from_tensor_slices(train_label)

ds_train = tf.data.Dataset.zip((ds_train_image, ds_train_label))

ds_train = ds_train.shuffle(10000).repeat().batch(64)

model = tf.keras.Sequential()

# 添加Flatten层将（28,28）的数据变成[28*28]
model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
model.add(tf.keras.layers.Dense(64, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.summary()

# sparse_categorical_crossentropy种类超过2两种，像这里面0到9游十种
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

steps_per_epochs = train_image.shape[0]//64
model.fit(ds_train, epochs=5, steps_per_epoch=steps_per_epochs)


